"""
steam峰值数据爬取
天津工业大学 大数据实训 第四组
杨玺锟
2020-1-10
"""

# -*- coding: utf-8 -*-
import scrapy
from ..items import SteamDataItem


class GameDataSpider(scrapy.Spider):
    name = 'game_data'
    allowed_domains = ['store.steampowered.com']
    start_urls = ['https://store.steampowered.com/stats']

    def parse(self, response):

        # 初始化item
        item = SteamDataItem()
        item['total'] = ''

        # 今日在线峰值
        total_str = response.xpath('/html/body/div[1]/div[7]/div[4]/div[1]/div[2]/div[2]/div/div[2]/div[2]/table/tr['
                                   '2]/td[3]/span/text()').extract()[0]  # 注意xpath中不要有tbody标签
        # print(total_str.replace(',',''))
        item['total'] = int(total_str.replace(',', ''))

        # 五个游戏峰值
        games_str = response.xpath('//*[@id="detailStats"]/table/tr')
        i = 1
        for game in games_str:
            game_str = game.xpath('./td[4]/a/text()').extract()
            if not game_str:
                continue
            else:
                i = i + 1
                game_str = game.xpath('./td[4]/a/text()').extract()[0]
                print(game_str)
                if game_str == 'Counter-Strike: Global Offensive':
                    csgo = game.xpath('./td[2]/span/text()').extract()[0]
                    print("csgo:%s" % csgo.replace(',', ''))
                    item['csgo'] = int(csgo.replace(',', ''))
                elif game_str == 'Dota 2':
                    dota2 = game.xpath('./td[2]/span/text()').extract()[0]
                    print(("dota2: %s" % dota2))
                    item['dota2'] = int(dota2.replace(',', ''))
                elif game_str == 'MONSTER HUNTER: WORLD':
                    mhw = game.xpath('./td[2]/span/text()').extract()[0]
                    print(("MHW: %s" % mhw))
                    item['mhw'] = int(mhw.replace(',', ''))
                elif game_str == 'PLAYERUNKNOWN\'S BATTLEGROUNDS':
                    pubg = game.xpath('./td[2]/span/text()').extract()[0]
                    print(("pubg: %s" % pubg))
                    item['pubg'] = int(pubg.replace(',', ''))
                elif game_str == 'Grand Theft Auto V':
                    gta5 = game.xpath('./td[2]/span/text()').extract()[0]
                    print(("gta5: %s" % gta5))
                    item['gta5'] = int(gta5.replace(',', ''))
                elif game_str == 'Tom Clancy\'s Rainbow Six Siege':
                    r6 = game.xpath('./td[2]/span/text()').extract()[0]
                    print(("r6: %s" % r6))
                    item['r6'] = int(r6.replace(',', ''))
                if i == 20:  # 只爬取前20个
                    break
        yield item
